import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:trina_grid/trina_grid.dart';

import '../../../widgets/text_button.dart';
import 'read_excel_controller.dart';

class ReadExcelPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<ReadExcelController>(
        init: ReadExcelController(),
        builder: (controller) {
          return Scaffold(
            backgroundColor: Colors.white,
            appBar: AppBar(title: Text('read_excel')),
            body: Center(
                child: Column(
              children: [
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    _textButton('读取excel', onPressed: () {
                      controller.readExcel();
                    }),
                    _textButton('刷新', onPressed: () {
                      controller.refreshData();
                    }),
                    _textButton('清空', onPressed: () {
                      controller.clear();
                    }),
                    _textButton('导出', onPressed: () {
                      controller.export();
                    }),
                  ],
                ),
                SizedBox(
                  height: 20,
                ),
                Expanded(
                    child: Container(
                        margin: EdgeInsets.symmetric(horizontal: 15),
                        child: TrinaGrid(
                          columns: controller.columns,
                          rows: controller.rows,
                          configuration: const TrinaGridConfiguration(
                              // columnSize: TrinaGridColumnSizeConfig(),
                              style: TrinaGridStyleConfig(
                                  activatedBorderColor:
                                      Colors.red, // 单元格被选中时的颜色
                                  // gridBackgroundColor: Colors.transparent,
                                  gridBorderColor: Colors.transparent,
                                  gridPadding: 0,
                                  rowHeight: 42,
                                  // gridPadding: 0,
                                  // gridBorderColor: Colors.black,
                                  defaultCellPadding: EdgeInsets.symmetric(
                                      horizontal: 1, vertical: 1),
                                  // oddRowColor: Colors.white,
                                  // evenRowColor: Color(0xFFE2F6DF),
                                  borderColor: Colors.transparent, // 单元格边线的颜色
                                  cellUnselectedColor: Colors.grey,
                                  // gridBackgroundColor: Colors.blue,
                                  cellCheckedColor: Colors.white,
                                  cellColorInEditState: Colors.white,
                                  cellTextStyle: TextStyle(color: Colors.blue),
                                  cellColorGroupedRow: Colors.yellow,
                                  columnTextStyle: TextStyle(
                                      color: Colors.white, // 表头的文字颜色
                                      fontWeight: FontWeight.bold))),
                          // rowColorCallback:
                          //     (TrinaRowColorContext rowColorContext) {
                          //   return Colors.white;
                          // },
                          onLoaded: (TrinaGridOnLoadedEvent event) {
                            controller.stateManager = event.stateManager;
                          },
                          mode: TrinaGridMode.select,
                        ))),
              ],
            )),
          );
        });
  }

  _excelData(ReadExcelController controller) {
    return Container(
        margin: EdgeInsets.symmetric(horizontal: 15),
        child: TrinaGrid(
          columns: controller.columns,
          rows: controller.rows,
          configuration: const TrinaGridConfiguration(
              // columnSize: TrinaGridColumnSizeConfig(),
              style: TrinaGridStyleConfig(
                  activatedBorderColor: Colors.red, // 单元格被选中时的颜色
                  // gridBackgroundColor: Colors.transparent,
                  gridBorderColor: Colors.transparent,
                  gridPadding: 0,
                  rowHeight: 42,
                  // gridPadding: 0,
                  // gridBorderColor: Colors.black,
                  defaultCellPadding:
                      EdgeInsets.symmetric(horizontal: 1, vertical: 1),
                  // oddRowColor: Colors.white,
                  // evenRowColor: Color(0xFFE2F6DF),
                  borderColor: Colors.transparent, // 单元格边线的颜色
                  cellUnselectedColor: Colors.grey,
                  // gridBackgroundColor: Colors.blue,
                  cellCheckedColor: Colors.white,
                  cellColorInEditState: Colors.white,
                  cellTextStyle: TextStyle(color: Colors.blue),
                  cellColorGroupedRow: Colors.yellow,
                  columnTextStyle: TextStyle(
                      color: Colors.white, // 表头的文字颜色
                      fontWeight: FontWeight.bold))),
          // rowColorCallback:
          //     (TrinaRowColorContext rowColorContext) {
          //   return Colors.white;
          // },
          onLoaded: (TrinaGridOnLoadedEvent event) {
            controller.stateManager = event.stateManager;
          },
          mode: TrinaGridMode.select,
        ));
  }

  _textButton(String text, {Function? onPressed}) {
    return Container(
        margin: EdgeInsets.only(left: 15, right: 20),
        alignment: Alignment.centerLeft,
        child: MyTextButton(
            text: text,
            confirm: () {
              onPressed?.call();
            },
            backgroundColor: Colors.deepPurpleAccent,
            textStyle: TextStyle(color: Colors.white)));
  }
}
